<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="家电名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入家电名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="家电品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入家电品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上市时间" prop="timetomarket">
        <el-date-picker clearable
                        v-model="queryParams.timetomarket"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择上市时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">



      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appliancesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="家电编号" align="center" prop="id" />
      <el-table-column label="家电名称" align="center" prop="name" />
      <el-table-column label="家电图片" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="家电库存" align="center" prop="count" />
      <el-table-column label="家电介绍" align="center" prop="content" />
      <el-table-column label="家电品牌" align="center" prop="brand" />
      <el-table-column label="产地" align="center" prop="place" />
      <el-table-column label="尺寸" align="center" prop="size" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="上市时间" align="center" prop="timetomarket" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timetomarket, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleRk(scope.row)"
          >新增订单</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家电管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家电名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入家电名称" />
        </el-form-item>
        <el-form-item label="家电图片">
          <image-upload v-model="form.pic"  :limit="1"/>
        </el-form-item>
        <el-form-item label="家电库存" prop="count">
          <el-input-number v-model="form.count" disabled  :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="家电介绍">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="家电品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入家电品牌" />
        </el-form-item>
        <el-form-item label="产地" prop="place">
          <el-input v-model="form.place" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="尺寸" prop="size">
          <el-input v-model="form.size" placeholder="请输入尺寸" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="上市时间" prop="timetomarket">
          <el-date-picker clearable
                          v-model="form.timetomarket"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择上市时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="kcopen" width="50%" append-to-body>
      <el-form ref="form" :model="formkc" :rules="rules" label-width="80px">
        <el-form-item label="购买数量" prop="count">
          <el-input-number v-model="formkc.count" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="客户名称" prop="clientname">
          <el-input v-model="formkc.clientname"  placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户电话" prop="clienttel">
          <el-input v-model="formkc.clienttel"  maxlength="11"  placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="客户地址" prop="clientaddress">
          <el-input v-model="formkc.clientaddress"  placeholder="请输入客户地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormkc">确 定</el-button>
        <el-button @click="kcopen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listAppliances, getAppliances, delAppliances, addAppliances, updateAppliances } from "@/api/system/appliances";
    import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/system/orders";

    export default {
        name: "Appliances",
        data() {
            return {
                jdcount:'',
                formkc:{},
                kcopen:false,
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 家电管理表格数据
                appliancesList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    name: null,
                    pic: null,
                    count: null,
                    content: null,
                    brand: null,
                    place: null,
                    size: null,
                    weight: null,
                    timetomarket: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    clientname: [
                        { required: true, message: "客户名称不能为空", trigger: "blur" }
                    ],
                    clienttel: [
                        { required: true, message: "客户电话不能为空", trigger: "blur" }
                    ],
                    count: [
                        { required: true, message: "家电库存不能为空", trigger: "blur" }
                    ],
                    clientaddress: [
                        { required: true, message: "客户地址不能为空", trigger: "blur" }
                    ],

                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询家电管理列表 */
            getList() {
                this.loading = true;
                listAppliances(this.queryParams).then(response => {
                    this.appliancesList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    name: null,
                    pic: null,
                    count: null,
                    createBy: null,
                    createTime: null,
                    content: null,
                    brand: null,
                    place: null,
                    size: null,
                    weight: null,
                    timetomarket: null
                };
                this.resetForm("form");
            },
            resetkc() {
                this.formkc = {
                    id: null,
                    type: null,
                    count: null,
                    tid:null
                };
                this.resetForm("formkc");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加家电管理";
            },
            handleRk(row){
                this.resetkc();
                const id = row.id || this.ids
                this.jdcount = row.count;
                this.formkc.tid = id
                this.title = "新增订单";
                this.kcopen = true;

            },
            handleCk(row){
                this.resetkc();
                const id = row.id || this.ids
                this.formkc.tid = id
                this.formkc.type = '出库'
                this.title = "出库";
                this.kcopen = true;
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getAppliances(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改家电管理";
                });
            },
            submitFormkc(){
                console.log('jdcount',parseInt(this.jdcount))
                console.log('count',parseInt(this.formkc.count))
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (parseInt(this.jdcount)>=parseInt(this.formkc.count)){
                            addOrders(this.formkc).then(response => {
                                console.log('res',response)
                                this.$modal.msgSuccess("订单创建成功");
                                this.kcopen = false;
                                this.getList();
                            });
                        }
                        else {
                            this.$modal.msgError("库存不足");

                        }
                    }
                });


            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateAppliances(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addAppliances(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除家电管理编号为"' + ids + '"的数据项？').then(function() {
                    return delAppliances(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            },
            /** 导出按钮操作 */
            handleExport() {
                this.download('system/appliances/export', {
                    ...this.queryParams
                }, `appliances_${new Date().getTime()}.xlsx`)
            }
        }
    };
</script>
